<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\Common\Constants\StatusConstants;
use App\CustHub\Model\ChSeat;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Mine\Abstracts\AbstractMapper;
use Mine\MineModel;

class ChSeatMapper extends AbstractMapper
{
    public function assignModel()
    {
        $this->model = ChSeat::class;
    }

    public function findById(mixed $id): ChSeat|MineModel|Model|Builder|null
    {
        return $this->model::query()->where('id',$id)->first();
    }

    /**
     * 获取可用席位
     * @param mixed $mainUid
     * @return ChSeat|MineModel|Model|Builder|null
     */
    public function getAvailableSeats(mixed $mainUid): ChSeat|MineModel|Model|Builder|null
    {
        $info = $this->model::query()
            ->where('main_uid', $mainUid)
            ->where('status', StatusConstants::ENABLE)
            ->where('use_status', StatusConstants::DISABLE)
            ->where('seat_type',0)
            ->where('expire_time', '>', time())
            ->first();
        return $info;
    }

    /**
     * 检查是否存在可用席位
     * @param mixed $mainUid
     * @return bool
     */
    public function checkIsAvailSeatExist(int $mainUid): bool
    {
        $count = $this->model::query()
            ->where('main_uid', $mainUid)
            ->where('status', StatusConstants::ENABLE)
            ->where('use_status', StatusConstants::DISABLE)
            ->where('seat_type',0)
            ->where('expire_time', '>', time())
            ->count();
        return $count > 0;
    }
}